// 快捷键rexR
import { getToken } from '@/utils/storage';

const initialState = {
  auth: getToken(),
  user: {} as UserInfo,
  profile: {} as ProFileType,
  //   refresh_token: '',
  // },
};

export type ProFileType = {
  birthday: string;
  gender: number;
  id: string;
  mobile: string;
  name: string;
  photo: string;
  intro: string;
};
export type AuthType = {
  token: string;
  refresh_token: string;
};
export type UserInfo = {
  id: string;
  name: string;
  photo: string;
  art_count: number;
  follow_count: number;
  fans_count: number;
  like_count: number;
};
type ActionType =
  | { type: 'user/saveAuth'; payload: AuthType }
  | { type: 'user/saveUserInfo'; payload: UserInfo }
  | { type: 'user/saveProfileInfo'; payload: ProFileType }
  // 变为可选类型
  | { type: 'user/uploadProfile'; payload: ProFileType }
  | { type: 'user/updateProfile'; payload: Partial<ProFileType> };

export default function userReducer(
  state = initialState,
  action: ActionType
): typeof initialState {
  switch (action.type) {
    case 'user/saveAuth':
      return {
        ...state,
        auth: action.payload,
      };
    case 'user/saveUserInfo':
      return {
        ...state,
        user: { ...action.payload },
      };
    case 'user/saveProfileInfo':
      return {
        ...state,
        profile: { ...action.payload },
      };
    case 'user/uploadProfile':
      return {
        ...state,
        profile: {
          ...state.profile, //把之前的放回去
          ...action.payload,
        },
      };
    case 'user/updateProfile':
      return {
        ...state,
        profile: {
          ...state.profile, //把之前的放回去
          ...action.payload,
        },
      };
    default:
      return state;
  }
}
